package com.bzy.shiro.util;

import javax.servlet.http.HttpServletRequest;

import com.bzy.common.config.Environment;
import com.bzy.common.util.LoggerUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.util.WebUtils;



public class SessionUtil {
	public static final String ATTRIBUTE_KEY_KICKOUT_SESSION = "kickout-session";
	public static final String ATTRIBUTE_KEY_FORCE_LOGOUT = "force-logout";
	public final static String ATTRIBUTE_KEY_OAUTH_APPKEY = "oauth-appkey";
	public final static String ATTRIBUTE_KEY_USER_ROLES = "user-roles";
	public final static String ATTRIBUTE_KEY_USER_PERMISSIONS = "user-permissions";
	public final static String ATTRIBUTE_KEY_VERIFY_CODE = "verify-code";
	public final static String ATTRIBUTE_KEY_USER_AIRPORTID_ID = "user-airport-id";
	public final static String ATTRIBUTE_KEY_USER_BUSINESS_ID = "user-business-id";
	public final static String ATTRIBUTE_KEY_USER_ID = "user-Id";
	public final static String ATTRIBUTE_KEY_ACCOUNT_ID = "account-Id";
	
	/**
	 * 获取当前用户的Session
	 * 
	 * @return
	 */
	public static Session getSession() {
		return SecurityUtils.getSubject().getSession();
	}

	/**
	 * 把值放入到当前登录用户的Session里
	 * 
	 * @param key
	 * @param value
	 */
	public static void setAttribute(Object key, Object value) {
		getSession().setAttribute(key, value);
	}

	/**
	 * 从当前登录用户的Session里取值
	 * 
	 * @param key
	 * @return
	 */
	public static Object getAttribute(Object key) {
		return getSession().getAttribute(key);
	}

	public static Object removeAttribute(Object key) {
		return getSession().removeAttribute(key);
	}
	
	public static String getSessionId() {
		return (String) getSession().getId();
	}
	
	public static Object getPrincipal() {
		return SecurityUtils.getSubject().getPrincipal();
	}
	
	/**
	 * 判断是否登录
	 * 
	 * @return
	 */
	public static boolean isLogin() {
		return SecurityUtils.getSubject().getPrincipal() != null;
	}

	/**
	 * 退出登录
	 */
	public static void logout() {
		SecurityUtils.getSubject().logout();
	}

	public static void setKickoutSession(Session session, Object value) {
		session.setAttribute(ATTRIBUTE_KEY_KICKOUT_SESSION, value);
	}

	public static Object getKickoutSession(Session session) {
		return session.getAttribute(ATTRIBUTE_KEY_KICKOUT_SESSION);
	}

	public static void setForceLogout(Session session, Object value) {
		session.setAttribute(ATTRIBUTE_KEY_FORCE_LOGOUT, value);
	}

	public static Object getForceLogout(Session session) {
		return session.getAttribute(ATTRIBUTE_KEY_FORCE_LOGOUT);
	}
	
	public static void setPrincipalCollection(Session session, Object value) {
		session.setAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY, value);
	}

	public static Object getPrincipalCollection(Session session) {
		return session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
	}

	public static void setSavedRequest(Object value) {
		setAttribute(WebUtils.SAVED_REQUEST_KEY, value);
	}

	public static Object getSavedRequest() {
		return getAttribute(WebUtils.SAVED_REQUEST_KEY);
	}

	public static void setOauthAppkey(Object value) {
		setAttribute(ATTRIBUTE_KEY_OAUTH_APPKEY, value);
	}

	public static Object getOauthAppkey() {
		return getAttribute(ATTRIBUTE_KEY_OAUTH_APPKEY);
	}

	public static void setUserRoles(Object value) {
		setAttribute(ATTRIBUTE_KEY_USER_ROLES, value);
	}

	public static Object getUserRoles() {
		return getAttribute(ATTRIBUTE_KEY_USER_ROLES);
	}

	public static void setUserPermissions(Object value) {
		setAttribute(ATTRIBUTE_KEY_USER_PERMISSIONS, value);
	}

	public static Object getUserPermissions() {
		return getAttribute(ATTRIBUTE_KEY_USER_PERMISSIONS);
	}
	
	public static void setVerifyCode(Object value) {
		setAttribute(ATTRIBUTE_KEY_VERIFY_CODE, value);
	}

	public static Object getVerifyCode() {
		return getAttribute(ATTRIBUTE_KEY_VERIFY_CODE);
	}

	public static void setUserAirportId(Object value) {
		setAttribute(ATTRIBUTE_KEY_USER_AIRPORTID_ID, value);
	}
	
	public static Object getUserAirportId() {
		Object userAirportId = getAttribute(ATTRIBUTE_KEY_USER_AIRPORTID_ID);
		LoggerUtil.info("...... this is userAirportId ..... "+userAirportId);
		return userAirportId;
	}
	
	public static void setUserBusinessId(Object value) {
		setAttribute(ATTRIBUTE_KEY_USER_BUSINESS_ID, value);
	}
	
	public static Object getUserBusinessId() {
		Object userBusinessId = getAttribute(ATTRIBUTE_KEY_USER_BUSINESS_ID);
		LoggerUtil.info("...... this is userBusinessId ..... "+userBusinessId);
		return userBusinessId;
	}
	
	/** 存放当前用户的userId **/
	public static void setUserId(String userId) {
		setAttribute(ATTRIBUTE_KEY_USER_ID, userId);
	}
	
	/** 获取用户的userId **/
	public static String getUserId() {
		Object userId = getAttribute(ATTRIBUTE_KEY_USER_ID);
		LoggerUtil.info("...... this is userId ..... "+userId);
		return (String) userId;
	}
	
	/** 存放后台用户的accountId **/
	public static void setAccountId(String accountId) {
		setAttribute(ATTRIBUTE_KEY_ACCOUNT_ID, accountId);
	}
	
	/** 获取后台用户的accountId **/
	public static String getAccountId() {
		String accountId = (String) getAttribute(ATTRIBUTE_KEY_ACCOUNT_ID);
		LoggerUtil.info("...... this is accountId ..... "+accountId);
		return accountId;
	}
	
	/*** 获取参数 sessionId ***/
	public static String getUserSessionIdByParam(HttpServletRequest request) {
		String sessionId = request.getParameter("usid");
		if (StringUtils.isBlank(sessionId)) {
			sessionId = request.getHeader("usid");
		}
		if (Environment.isDevelopment()) {
			sessionId = request.getParameter("d_usid");
			if (StringUtils.isBlank(sessionId)) {
				sessionId = request.getHeader("d_usid");
			}
		}
		LoggerUtil.info("...... this is sessionId ..... "+sessionId);
		return sessionId;
	}
}
